package com.gxa.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.R;
import com.gxa.finance.mapper.UserAccountMapper;
import com.gxa.finance.pojo.UserAccount;
import com.gxa.finance.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Override
    public R addUserAccount(UserAccount userAccount) {
        R r = new R();
        userAccount.setCreateTime(new Timestamp(System.currentTimeMillis()));
        try {
            int result = userAccountMapper.insert(userAccount);
            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("添加失败");
        }
        return r;
    }

    @Override
    public R updateUserAccount(UserAccount userAccount) {
        R r = new R();
        try {
            int result = userAccountMapper.updateById(userAccount);
            if (result>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("修改失败");
        }
        return r;
    }

    @Override
    public List<UserAccount> findByUaNameAccount(String uaName) {

            QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uaName",uaName);
            List<UserAccount> userAccounts = userAccountMapper.selectList(queryWrapper);

        return userAccounts;
    }

    @Override
    public R updateUserAccountMoney(String account, BigDecimal money) {
        R r = new R();
        try {
            UpdateWrapper<UserAccount> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("account",account).setSql("money = money-"+money);
            super.update(updateWrapper);
            r.setCode(200);
            r.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("修改失败");
        }
        return r;
    }
}
